package com.zking.ssm.DYM.controller;

import com.zking.ssm.DYM.model.HotelRoom;
import com.zking.ssm.DYM.service.IHotelRoomService;
import com.zking.ssm.util.JsonData;
import com.zking.ssm.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author XXXXX
 * @Date 2021/01/19 20:10
 */
@Controller
@RequestMapping("/hotelRoom")
public class HotelRoomController {
    @Autowired
    private IHotelRoomService hotelRoomService;
    private PageBean pageBean;

    @ModelAttribute
    public void init(Model model){
        System.out.println("init");
        HotelRoom hotelRoom = new HotelRoom();
        model.addAttribute("hotelRoom",hotelRoom);
    }
    //添加数据
    @RequestMapping("/toAddHotelRoom")
    public String toAddHotelRoom(Model model) {
        System.out.println("toAddHotelRoom");
        return "addHotelRoom";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(HotelRoom hotelRoom){
        System.out.println("add");
        hotelRoomService.insert(hotelRoom);
        return "redirect:toAddHotelRoom";
    }

    //修改数据
    //① 更新酒店房间，先通过ID找到酒店房间
    @RequestMapping("/updateHotelRoom/{id}")
    public String updateHotelRoom(@PathVariable("id") int id,Model model){
        model.addAttribute("updateHotelRoom",hotelRoomService.selectByPrimaryKey(id));
        return "updateHotelRoom";
    }
    //② 然后修改，点击更新提交数据给后端
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(HotelRoom hotelRoom){
        hotelRoomService.updateByPrimaryKey(hotelRoom);
        return "redirect:updateHotelRoom";
    }

    //删除酒店房间
    @RequestMapping(value = "/deleteHotelRoomById")
    @ResponseBody
    public String deleteHotelRoomById(Integer id){
        hotelRoomService.deleteByPrimaryKey(id);
        System.out.println(id);
        return "redirect:HotelRoom";
//        return "删除成功";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public JsonData list(HotelRoom hotelRoom,PageBean pageBean){
        System.out.println("list");
        List<HotelRoom> hotelRoomList = hotelRoomService.list(hotelRoom, pageBean);
//      System.out.println("集合:"+hotelRoomList);
        JsonData jsonData = new JsonData();
        jsonData.setCode(0);
        jsonData.setPage(pageBean.getPage());
        jsonData.setRows(pageBean.getRows());
        jsonData.setTotal(pageBean.getTotal());
        jsonData.setMessage("查询成功");
        jsonData.setResult(hotelRoomList);
        return jsonData;
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public JsonData get(HotelRoom hotelRoom){
        final HotelRoom h = hotelRoomService.selectByPrimaryKey(hotelRoom.getId());
        JsonData jsonData = new JsonData();
        jsonData.setCode(0);
        jsonData.setMessage("查询成功");
        jsonData.setResult(h);
        return jsonData;
    }
}
